package adapters;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.newxlmedical.R;

import java.util.List;

import beans.WarningInfoBean;
import butterknife.BindView;
import butterknife.ButterKnife;
import custom.BaseRecyclerAdapter;

/**
 * Created by Administrator on 2018/11/12.
 */

public class ComboModifyAndAddAdapter extends BaseRecyclerAdapter<ComboModifyAndAddAdapter.ComboModifyAndAddViewHolder> {

    private Context mContext;
    private List<WarningInfoBean.DataBean> list;
    private ComboMofifyAndAddListener listener;

    public ComboModifyAndAddAdapter(Context mContext, List<WarningInfoBean.DataBean> list,ComboMofifyAndAddListener listener) {
        this.mContext = mContext;
        this.list = list;
        this.listener = listener;
    }

    @Override
    public ComboModifyAndAddAdapter.ComboModifyAndAddViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.adapter_combo_modify_and_add, parent, false);
        return new ComboModifyAndAddViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(final ComboModifyAndAddAdapter.ComboModifyAndAddViewHolder viewHolder, final int position) {
        final WarningInfoBean.DataBean dataBean = list.get(position);
        viewHolder.tvName.setText(dataBean.getGoodName());
        viewHolder.tvSpecification.setText(dataBean.getSpecification());
        viewHolder.tvSuppier.setText(dataBean.getManufacturer());

        if (dataBean.isComboMember()){
            viewHolder.cbCheck.setChecked(true);
        }else {
            viewHolder.cbCheck.setChecked(false);
        }

        if (position % 2 == 0) {
            viewHolder.llSearchAdapter.setBackgroundColor(mContext.getResources().getColor(R.color.partLineBackground));
        } else {
            viewHolder.llSearchAdapter.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }

        viewHolder.llCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewHolder.cbCheck.setChecked(!viewHolder.cbCheck.isChecked());
                // 要设置 不然复用item时 选中的就会被取消
                list.get(position).setComboMember(viewHolder.cbCheck.isChecked());
                listener.onCheckListener(viewHolder.cbCheck.isChecked(),dataBean.getGID());
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ComboModifyAndAddViewHolder extends BaseRecyclerAdapter.BaseViewHolder {
        @BindView(R.id.cb_adapter_combo_modify_and_add_check)
        CheckBox cbCheck;
        @BindView(R.id.tv_adapter_combo_modify_and_add_name)
        TextView tvName;
        @BindView(R.id.tv_adapter_combo_modify_and_add_specification)
        TextView tvSpecification;
        @BindView(R.id.tv_adapter_combo_modify_and_add_suppier)
        TextView tvSuppier;
        @BindView(R.id.ll_search_adapter)
        LinearLayout llSearchAdapter;
        @BindView(R.id.ll_adapter_combo_modify_and_add_check)
        LinearLayout llCheck;

        public ComboModifyAndAddViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

   public interface ComboMofifyAndAddListener{
        void onCheckListener(boolean isChecked, int id);
    }
}
